<template>
	<main-container>
		<el-card>
			<template #header>
				<div>
					<span>文件存储配置</span>
				</div>
			</template>
			<el-form ref="formRef" :model="state.form" :rules="state.rules" label-width="140px">
				<el-form-item label="文件存储类型" prop="type">
					<el-radio-group v-model="state.form.type">
						<el-radio label="1">阿里云OSS</el-radio>
						<el-radio label="2">七牛云</el-radio>
						<el-radio label="3">腾讯COS</el-radio>
						<el-radio label="4">MinIO</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-row>
					<el-col :span="12" class="mb20">
						<el-form-item label="AccessKey" prop="accessKey">
							<el-input v-model="state.form.accessKey"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12" class="mb20">
						<el-form-item label="AccessKeySecret" prop="accessSecret">
							<el-input v-model="state.form.accessSecret"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="12" class="mb20">
						<el-form-item label="endpoint" prop="endpoint">
							<el-input v-model="state.form.endpoint"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12" class="mb20">
						<el-form-item label="bucket" prop="bucket">
							<el-input v-model="state.form.bucket"></el-input>
						</el-form-item>
					</el-col>
				</el-row>

				<span class="hx-card-footer">
					<el-button type="primary" @click="submitForm(formRef)" :loading="loading">保 存</el-button>
					<el-button @click="resetForm(formRef)">重 置</el-button>
				</span>
			</el-form>
		</el-card>
	</main-container>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue';
import { getObj, editObj } from '/@/api/upms/storageconfig';
import { ElMessage, FormInstance } from 'element-plus';
const state = reactive({
	form: { type: '', accessKey: '', accessSecret: '', endpoint: '', bucket: '', dir: '' },
	rules: {
		type: [
			{
				required: true,
				message: '请选择文件存储类型',
				trigger: 'change',
			},
		],
		accessKey: [
			{
				required: true,
				message: '请输入AccessKey',
				trigger: 'change',
			},
		],
		accessSecret: [
			{
				required: true,
				message: '请输入AccessKeySecret',
				trigger: 'change',
			},
		],
		endpoint: [
			{
				required: true,
				message: '请输入endpoint',
				trigger: 'change',
			},
		],
		bucket: [
			{
				required: true,
				message: '请输入bucket',
				trigger: 'change',
			},
		],
	},
});
const loading = ref(false);
const formRef = ref();
const initConfig = () => {
	loading.value = true;
	getObj()
		.then((response: any) => {
			loading.value = false;
			state.form = response.data.data;
		})
		.catch(() => {
			loading.value = false;
		});
};
/**
 * 修改配置
 */
const submitForm = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid, fields) => {
		if (valid) {
			loading.value = true;
			editObj(state.form)
				.then(() => {
					ElMessage.success('保存成功');
					loading.value = false;
				})
				.catch(() => {
					loading.value = false;
				});
		}
	});
};
/**
 * 重置表单
 */
const resetForm = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	loading.value = false;
	formEl.resetFields();
};
initConfig();
</script>
<style lang="scss" scoped></style>
